package co.yixiang.modules.shop.rest;

import co.yixiang.dozer.service.IGenerator;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.aop.NoRepeatSubmit;
import co.yixiang.modules.shop.domain.YxStoreFeedback;
import co.yixiang.modules.shop.service.YxStoreFeedBackService;
import co.yixiang.modules.shop.service.dto.YxStoreFeedBackQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

/**
 * @author fanfan
 * @date 2022-02-25 16:25
 */

@Api(tags = "反馈建议管理")
@RestController
@RequestMapping("/api/yxStoreFeedBack")
public class StoreFeedBackControll {

    private final YxStoreFeedBackService yxStoreFeedBackService;
    private final IGenerator generator;

    public StoreFeedBackControll(YxStoreFeedBackService yxStoreFeedBackService, IGenerator generator) {
        this.yxStoreFeedBackService = yxStoreFeedBackService;
        this.generator = generator;
    }

    @GetMapping
    @Log("查询反馈建议管理")
    @ApiOperation("查询反馈建议管理")
    @PreAuthorize("@el.check('YXSTOREFEEDBACK_SELECT')")
    public ResponseEntity<Object> getYxStoreFeedBack(YxStoreFeedBackQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(yxStoreFeedBackService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @NoRepeatSubmit
    @PostMapping
    @Log("新增反馈管理")
    @ApiOperation("新增反馈管理")
    @PreAuthorize("@el.check('YXSTOREFEEDBACK_ADD')")
    public ResponseEntity<Object> create(@Validated @RequestBody YxStoreFeedback resources) {
        return new ResponseEntity<>(yxStoreFeedBackService.save(resources), HttpStatus.CREATED);
    }

    @NoRepeatSubmit
    @PutMapping
    @Log("修改反馈管理")
    @ApiOperation("修改反馈管理")
    @PreAuthorize("hasAnyRole('YXSTOREFEEDBACK_EDIT')")
    public ResponseEntity<Object> update(@Validated @RequestBody YxStoreFeedback resources) {
        if (resources.getDisposeBy().length() > 1) {
            resources.setDisposeTime(new Date());
        }
        yxStoreFeedBackService.saveOrUpdate(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @NoRepeatSubmit
    @Log("删除反馈管理")
    @ApiOperation("删除反馈管理")
    @PreAuthorize("@el.check('YXSTOREFEEDBACK_DEL')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        yxStoreFeedBackService.removeByIds(new ArrayList<>(Arrays.asList(ids)));
        return new ResponseEntity<>(HttpStatus.OK);
    }
}